<?php
    include "../../../config.php";
    require_once("../redirect.php");
    require_once("../config.php");
    global $FOLIO_CFG;

    Header("content-type: application/x-javascript");

    $path = $CFG->wwwroot . 'mod/folio/ajax/';
    $url = $CFG->wwwroot;

    // Get the correct fieldname for the echo var in the Redirect object.
    //      This is then passed as the name of the parameter so that
    //      the ajax pages know what to do as a resulting action.
    $redirect_echo = folio_redirect::htmlVariableName( 'echo' );

?>
// code from internet example of edit.php & scriptaculous
// http://24ways.org/2005/edit-in-place-with-ajax
//function init(){
//    makeEditable('desc');
//    makeEditable('pizza');
//}

// Used to make a normal textarea editable.
function folio_ajax_makeEditable(iIdent, sType, sField, id)
{
    Event.observe(id, 'click', function(){folio_ajax_edit(iIdent, sType, sField, $(id),'')}, false);
    Event.observe(id, 'mouseover', function(){folio_ajax_showAsEditable($(id))}, false);
    Event.observe(id, 'mouseout', function(){folio_ajax_showAsEditable($(id), true)}, false);
}

// Used to make a textarea editable with a gui
function folio_ajax_makeRichEditable(iIdent, sType, sField, id)
{
    Event.observe(id, 'click', function(){folio_ajax_Richedit(iIdent, sType, sField, $(id))}, false);
    Event.observe(id, 'mouseover', function(){folio_ajax_showAsEditable($(id))}, false);
    Event.observe(id, 'mouseout', function(){folio_ajax_showAsEditable($(id), true)}, false);
}

// Used to pass in custom html for the edit control
function folio_ajax_makeHtmlEditable(iIdent, sType, sField, id, sHtml)
{
    Event.observe(id, 'click', function(){folio_ajax_edit(iIdent, sType, sField, $(id), sHtml)}, false);
    Event.observe(id, 'mouseover', function(){folio_ajax_showAsEditable($(id))}, false);
    Event.observe(id, 'mouseout', function(){folio_ajax_showAsEditable($(id), true)}, false);
}

function folio_ajax_edit(iIdent, sType, sField, obj, sHtml ){
    Element.hide(obj);

    var iRows = 1;
    var textarea = '';

    if (sHtml == '') {
        // Normal textarea
        textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="'+iRows+'" cols="60">'+obj.innerHTML+'</textarea>';
    } else {
        // Custom, using a normal text entry input box.
        textarea = '<div id="'+obj.id+'_editor"><input type="text" id="'+obj.id+'_edit" name="'+obj.id+'" value="'+obj.innerHTML+'" ><br/>';
    }

    var button     = '<div><input id="'+obj.id+'_save" type="button" value="SAVE" /> OR <input id="'+obj.id+'_cancel" type="button" value="CANCEL" /><br/></div></div>';
    var sField = '<textarea id="'+obj.id+'_field" style="display:none;">'+sField+'</textarea>';

    new Insertion.After(obj, textarea+sField+button);

    Event.observe(obj.id+'_save', 'click', function(){folio_ajax_saveChanges(iIdent, sType, obj)}, false);
    Event.observe(obj.id+'_cancel', 'click', function(){folio_ajax_cleanUp(iIdent, sType, obj)}, false);

    // Run passed js code.
    if ( sHtml != '') {
        eval(sHtml);
    }

}

// For using tinymce as the editor.
function folio_ajax_Richedit(iIdent, sType, sField, obj){
    Element.hide(obj);

    var iRows = 4;

    var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="'+iRows+'" cols="60">'+obj.innerHTML+'</textarea>';
    var button     = '<div><input id="'+obj.id+'_save" type="button" value="SAVE" /> OR <input id="'+obj.id+'_cancel" type="button" value="CANCEL" /><br/></div></div>';
    var sField = '<textarea id="'+obj.id+'_field" style="display:none;">'+sField+'</textarea>';

    new Insertion.After(obj, textarea+sField+button);

    Event.observe(obj.id+'_save', 'click', function(){folio_ajax_saveRichChanges(iIdent, sType, obj)}, false);
    Event.observe(obj.id+'_cancel', 'click', function(){folio_ajax_RichcleanUp(iIdent, sType, obj)}, false);

    // For firefox, set counter=0, otherwise the 2nd editor won't load.
    tinyMCE.idCounter=0;
    tinyMCE.execCommand('mceAddControl', true, obj.id);
    Element.hide(obj.id+"_edit");


}

// For using tinyMCE
//      Errors when using inside function, be sure to keep in the top level of this js file.
tinyMCE.init({
    mode:"textareas",
        plugins : 'spellchecker,emotions,contextmenu,preview,style,searchreplace,safari',
        convert_urls : false,
        relative_urls : false,
        theme : "advanced",
        theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,image,undo,redo,link,unlink,code,spellchecker,emotions",
        theme_advanced_buttons2 : "preview,styleprops,search,replace",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_path_location : "bottom",
        plugin_preview_width : "500",
        plugin_preview_height : "600",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
        remove_linebreaks: true,
        theme_advanced_source_editor_width : "400",
        theme_advanced_source_editor_height : "400",
        spellchecker_languages : "en",
        document_base_url : "<?php echo $url ?>",
        width : "<?php echo ($FOLIO_CFG->page_edit_width); ?>",
        height : "<?php echo $FOLIO_CFG->page_edit_height; ?>",
        fullscreen_settings : {
            theme_advanced_path_location : "top"
        }
});

function folio_ajax_showAsEditable(obj, clear){
    if (!clear){
        Element.addClassName(obj, 'folio_page_editable_highlight');
        Element.removeClassName(obj, 'folio_page_editable');
    }else{
        Element.removeClassName(obj, 'folio_page_editable_highlight');
        Element.addClassName(obj, 'folio_page_editable');
    }
}


function folio_ajax_saveRichChanges(iIdent, sType, obj){

    var new_content    =  encodeURIComponent(tinyMCE.getContent());
    var sField =  encodeURIComponent($F(obj.id+'_field'));

    obj.innerHTML    = "Saving...";
    folio_ajax_RichcleanUp(iIdent, sType, obj, false);

    var success    = function(t){folio_ajax_editComplete(t, obj);}
    var failure    = function(t){folio_ajax_editFailed(t, obj);}

      var url = '<?php echo $path; ?>'+sType+'_update.php';
    var pars = sType+'_ident=' + iIdent + '&<?php echo $redirect_echo; ?>=' + sField + '&content='+new_content;
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}
function folio_ajax_RichcleanUp(iIdent, sType, obj, keepEditable){

    // For removing editor.
    tinyMCE.execCommand("mceRemoveControl", false, obj.id);

    Element.remove(obj.id+'_editor');
    Element.show(obj);
    if (!keepEditable) folio_ajax_showAsEditable(obj, true);
}


function folio_ajax_saveChanges(iIdent, sType, obj){

    var new_content    =  encodeURIComponent($F(obj.id+'_edit'));
    var sField =  encodeURIComponent($F(obj.id+'_field'));

    obj.innerHTML    = "Saving...";
    folio_ajax_cleanUp(iIdent, sType, obj, false);

    var success    = function(t){folio_ajax_editComplete(t, obj);}
    var failure    = function(t){folio_ajax_editFailed(t, obj);}

      var url = '<?php echo $path; ?>'+sType+'_update.php';
    var pars = sType+'_ident=' + iIdent + '&<?php echo $redirect_echo; ?>=' + sField + '&content='+new_content;
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function folio_ajax_cleanUp(iIdent, sType, obj, keepEditable){

    // For removing editor.
    Element.remove(obj.id+'_editor');
    Element.show(obj);
    if (!keepEditable) folio_ajax_showAsEditable(obj, true);
}

function folio_ajax_editComplete(t, obj){
    obj.innerHTML    = t.responseText;
    folio_ajax_showAsEditable(obj, true);
}

function folio_ajax_editFailed(t, obj){
    obj.innerHTML    = 'Sorry, the update failed.';
    folio_ajax_cleanUp(obj);
}
